import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class 连接 {


    @Test
    public void 连接方式一() {
        try {
            //1.提供java.sql.Driver接口实现类的对象
            Driver driver = null;
            driver = new Driver();

            //2.提供url，指明具体操作的数据
            String url = "jdbc:mysql://123.56.20.87:13306/test";

            //3.提供Properties的对象，指明用户名和密码
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "123456");

            //4.调用driver的connect()，获取连接
            Connection conn = driver.connect(url, info);

            System.out.println(conn);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void 连接方式二() {
        try {
            //1.实例化Driver
            String className = "com.mysql.cj.jdbc.Driver";
            Class clazz = Class.forName(className);

            java.sql.Driver driver = (java.sql.Driver) clazz.newInstance();

            //2.提供url，指明具体操作的数据
            String url = "jdbc:mysql://123.56.20.87:13306/test";

            //3.提供Properties的对象，指明用户名和密码
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "123456");

            //4.调用driver的connect()，获取连接
            Connection conn = driver.connect(url, info);

            System.out.println(conn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void 连接方式三() {
        try {
            //1.数据库连接的4个基本要素：
            String url = "jdbc:mysql://123.56.20.87:13306/test";
            String user = "root";
            String password = "123456";
            String driverName = "com.mysql.cj.jdbc.Driver";

            //2.实例化Driver
            Class<?> clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();

            //3.注册驱动
            DriverManager.registerDriver(driver);

            //4.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);

            System.out.println(conn);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void 连接方式四() {
        try {
            //1.数据库连接的4个基本要素：
            String url = "jdbc:mysql://123.56.20.87:13306/test";
            String user = "root";
            String password = "123456";
            String driverName = "com.mysql.cj.jdbc.Driver";

            //2.加载Driver
            Class.forName(driverName);  //加载类会调用类中静态代码块

            //4.获取连接
            Connection conn = DriverManager.getConnection(url, user, password);

            System.out.println(conn);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void 链接方式五() throws Exception {
        //1.加载配置文件
        InputStream is = 连接.class.getClassLoader().getResourceAsStream("resources/jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        //2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //3.加载驱动
        Class.forName(driverClass);

        //4.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);

        System.out.println(conn);
    }

    public static Connection getConnection() throws Exception {
        //1.加载配置文件
        InputStream is = 连接.class.getClassLoader().getResourceAsStream("resources/jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);

        //2.读取配置信息
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");

        //3.加载驱动
        Class.forName(driverClass);

        //4.获取连接
        Connection conn = DriverManager.getConnection(url, user, password);


        return conn;
    }

}
